package com.ilovepostcard;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;

import com.ilovepostcard.model.Postcard;
import com.ilovepostcard.view.PostcardGridView;
import com.ilovepostcard.view.PostcardGridView.OnPostcardClickListener;

public class SearchResultPostCardActivity extends BaseActivity implements PostcardGridView.OnPostcardDataChangeListener{

	private final static int DLG_ID_PROCESSING = 3;
	
	ListView list;
	String key;
	TextView result;
	PostcardGridView searchlist;
	@Override
    public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		key = this.getIntent().getStringExtra("key");
		setContentView(R.layout.activity_search_result_postcard);
		
		init();
	}
	
	protected Dialog onCreateDialog( int id )
    {
        if( id == DLG_ID_PROCESSING )
        {
            ProgressDialog processDlg = new ProgressDialog( this );
            processDlg.setMessage(getResources().getString(R.string.loading));
            processDlg.setIndeterminate( true );
            processDlg.setCancelable( false );
			return processDlg;
        }       

        return super.onCreateDialog( id );
	}	
	
	private void init()
	{
		result = (TextView)findViewById(R.id.search_result);
		searchlist = (PostcardGridView)findViewById(R.id.search_pc);
		searchlist.setOnPostcardClickListener(new OnPostcardClickListener() {
            
            @Override
            public void onClick(Postcard postcard) {
                Intent intent = new Intent(SearchResultPostCardActivity.this, PostcardActivity.class);
                intent.putExtra("postcard", postcard);
                startActivity(intent);
            }
        });
		searchlist.setKeyWord(key);
		searchlist.setOnPostcardDataChangeListener(this);
		searchlist.loadNextPage();
		
		String message = this.getResources().getString(R.string.search_result_pc);
		String formatMSG = String.format(message, searchlist.getGridView().getCount());
		result.setText(formatMSG);
		
		
	}
	

	@Override
	public void onDataChange() {
		String message = this.getResources().getString(R.string.search_result_pc);
		String formatMSG = String.format(message, searchlist.getGridView().getCount());
		result.setText(formatMSG);
		
	}
}
